package com.cdream.basic.thread;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author cdream
 * @date 2022/2/13
 */
public class AtomicExample {
    public static void main(String[] args) throws InterruptedException {
        AtomicReference<Dog> dog = new AtomicReference<>(new Dog("小白", "13"));
        Dog expect = dog.get();
        new Thread(()-> {
            expect.age = "11";
        }).start();
        Thread.sleep(1000);
        System.out.println(dog.compareAndSet(expect, new Dog("小黑", "小王")));

        //改动对象版本身引用
        AtomicStampedReference<String> asr = new AtomicStampedReference<>("小白", 0);
        // 保护改动数组内对象
        AtomicReferenceArray<Dog> dogArray = new AtomicReferenceArray<>(new Dog[]{});
        // 更新器
        AtomicReferenceFieldUpdater<Dog, String> name = AtomicReferenceFieldUpdater.newUpdater(Dog.class, String.class, "name");


    }
}
